package com.health.service.impl;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.health.model.BloodPressure;
import com.health.service.BloodPressureService;

public class BloodPressureServiceImpl implements BloodPressureService {
	
	private static SqlSessionFactory factory;
	private SqlSession session = null;
	
	static Log log = LogFactory.getLog(BloodPressureService.class);
	
	static {
		String resource = "sqlMapConfig.xml";
		try {
			Reader reader = Resources.getResourceAsReader(resource);
			factory = new SqlSessionFactoryBuilder().build(reader);
			reader.close();
		} catch (IOException e) {
			log.error(e.getCause());
		}
	}
	

	@Override
	public int insert(BloodPressure pressure) {
		session = factory.openSession();
		BloodPressureService service = session.getMapper(BloodPressureService.class);
		
		int flag = service.insert(pressure);
		session.commit();
		if(flag==1) {
			log.info("--INSERT SUCCESS(BloodPressureService)");
			log.info(pressure.toString());
		} else {
			log.info("==INSERT FAIL(BloodPressureService)== flag = " + flag);
		}
		session.close();
		return flag;				//1:succeed		0:failed
	}

	@Override
	public int deleteByPK(String id) {
		session = factory.openSession();
		BloodPressureService service = session.getMapper(BloodPressureService.class);
		int flag = service.deleteByPK(id);
		if(flag == 1) {
			log.info("----- DELETE SUCCEED(BloodPressureService) -----");
		} else {
			log.info("----- DELETE FAILED(BloodPressureService) -----");
		}
		session.commit();
		session.close();
		return flag;
		
	}

	
	@Override
	public int updateByPK(BloodPressure p) {
		session = factory.openSession();
		BloodPressureService service = session.getMapper(BloodPressureService.class);
		int updateFlag = service.updateByPK(p);
		session.commit();
		if(updateFlag == 1) {
			log.info("---- UPDATE SUCCEED(BloodPressureService) ----");
		} else {
			log.info("---- UPDATE FAILED(BloodPressureService) ----");
		}
		return updateFlag;
	}

	@Override
	public BloodPressure selectByPK(String id) {
		session = factory.openSession();
		BloodPressureService service = session.getMapper(BloodPressureService.class);
		
		BloodPressure p = service.selectByPK(id);
		session.commit();
		log.info("----- SELECT BY PK(BloodPressureService) -----");
		log.info(p);
		session.close();
		return p;
	}

	/**
	 * 条件查询,low,high,examtime...(scope)
	 * 当前用户
	 * 如果是管理员,则查所有用户的所有记录(将用户增加为查询条件)
	 * */
	@Override
	public List<BloodPressure> selectForList(BloodPressure p) {
		session = factory.openSession();
		BloodPressureService service = session.getMapper(BloodPressureService.class);
		
		List<BloodPressure> list = service.selectForList(p);
		session.commit();
		log.info("----- SelectForList(BloodPressureService) -----" + list.size());
//		Iterator<BloodPressure> it = list.iterator();
//		while(it.hasNext()) {
//			System.out.println("\t" + it.next());
//		}
		session.close();
		return list;
	}
	
	/**
	 * 查出所有记录.无条件
	 * */
	@Override
	public List<BloodPressure> selectList() {
		session = factory.openSession();
		BloodPressureService service = session.getMapper(BloodPressureService.class);
		
		List<BloodPressure> list = service.selectList();
		session.commit();
		log.info("----- SelectForList(BloodPressureService) -----" + list.size());
//		Iterator<BloodPressure> it = list.iterator();
//		while(it.hasNext()) {
//			System.out.println("\t" + it.next());
//		}
		session.close();
		
		return list;
	}
	

}
